package matt.design;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

//LOOK UP FACTORY DESIGN PATTERNS

/**
 * This class determines what happens when various buttons on the login page are
 * clicked and the linking screens
 * 
 * @author Crunch Time
 * @version 1.0
 */
public class LoginActivity extends Activity {

	private UsersDataSource datasource;
	
	@Override
	/**
	 * This method determines what happens when a button is 
	 * clicked on the login page
	 * 
	 * @param savedInstanceState
	 */
	public void onCreate(Bundle savedInstanceState) {
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		datasource = new UsersDataSource(this);
		datasource.open();
		
		Button loginAction = (Button) findViewById(R.id.btn_login);
		Button registerScreen = (Button) findViewById(R.id.btn_register);
		Button passwordFunctionsScreen = (Button) findViewById(R.id.btn_password);
		
		loginAction.setOnClickListener(new View.OnClickListener() {

			EditText user = (EditText) findViewById(R.id.username);
			EditText pass = (EditText) findViewById(R.id.password);
			TextView errortxt = (TextView)findViewById(R.id.login_errortxt);
			/**
			 * Determines what happens when the login button is clicked
			 * 
			 * @param v
			 */
			public void onClick(View v) {
				
				errortxt.setText("");
				//UserManager users = UserManager.getUsers();
				String username = user.getText().toString();
				String password = pass.getText().toString();
				//boolean check = users.checkLogin(username, password);
				if (datasource.containsUser(username)) {
					if (datasource.checkPassword(username, password)) {
						user.setText("");
						pass.setText("");
						HomepageActivity.CURRENT_USER = username;
						startActivity(new Intent(getApplicationContext(), HomepageActivity.class));
					}
					else {
						//throw some error
						TextView errortxt = (TextView)findViewById(R.id.login_errortxt);
						errortxt.setText("Incorrect Email or Password");
					}
				}
				else {
					//throw some error
					TextView errortxt = (TextView)findViewById(R.id.login_errortxt);
					errortxt.setText("No User with that Username");
				}
			}
		});
		
		// Listening to register new account link
		registerScreen.setOnClickListener(new View.OnClickListener() {

			EditText user = (EditText) findViewById(R.id.username);
			EditText pass = (EditText) findViewById(R.id.password);
			TextView errortxt = (TextView)findViewById(R.id.login_errortxt);
			/**
			 * Determines what happens when the new user button is clicked
			 * 
			 * @param v
			 */
			public void onClick(View v) {
				// Switching to Register screen
				
				errortxt.setText("");
				user.setText("");
				pass.setText("");
				startActivity(new Intent(getApplicationContext(), RegisterActivity.class));
			}
		});

		passwordFunctionsScreen.setOnClickListener(new View.OnClickListener() {

			EditText user = (EditText) findViewById(R.id.username);
			EditText pass = (EditText) findViewById(R.id.password);
			TextView errortxt = (TextView)findViewById(R.id.login_errortxt);
			/**
			 * Determines what happens when the password functions button is clicked
			 * 
			 * @param v
			 */
			public void onClick(View v) {
				// Switching to Register screen
				
				errortxt.setText("");
				user.setText("");
				pass.setText("");
				startActivity(new Intent(getApplicationContext(), PasswordFunctionActivity.class));
			}
		});
		
	}
}